package org.jim.server.command.handler;

import org.apache.commons.lang3.StringUtils;
import org.jim.core.ImChannelContext;
import org.jim.core.ImPacket;
import org.jim.core.ImStatus;
import org.jim.core.config.ImConfig;
import org.jim.core.exception.ImException;
import org.jim.core.message.MessageHelper;
import org.jim.core.packets.ApplyLogReqBody;
import org.jim.core.packets.Command;
import org.jim.core.packets.RespBody;
import org.jim.core.utils.JsonKit;
import org.jim.server.command.AbstractCmdHandler;
import org.jim.server.config.ImServerConfig;
import org.jim.server.protocol.ProtocolManager;

import java.util.Objects;

/**
 * 获取好友申请记录信息
 */
public class ApplyLogReqHandler extends AbstractCmdHandler {

    @Override
    public Command command() {
        return Command.COMMAND_APPLY_LOG_REQ;
    }

    @Override
    public ImPacket handler(ImPacket packet, ImChannelContext imChannelContext) throws ImException {
        RespBody resPacket = new RespBody(Command.COMMAND_APPLY_LOG_REQ, ImStatus.C10024);
        ApplyLogReqBody applyLogReqBody = JsonKit.toBean(packet.getBody(), ApplyLogReqBody.class);
        if (Objects.isNull(applyLogReqBody)) {
            return ProtocolManager.Packet.dataInCorrect(Command.COMMAND_APPLY_LOG_RESP, ImStatus.C10020, imChannelContext);
        }
        ImServerConfig imServerConfig = ImConfig.Global.get();
        MessageHelper messageHelper = imServerConfig.getMessageHelper();
        String userId = applyLogReqBody.getUserId();
        if (StringUtils.isBlank(userId)) {
            return ProtocolManager.Packet.dataInCorrect(Command.COMMAND_APPLY_LOG_RESP, ImStatus.C10020, imChannelContext);
        }
        resPacket.setData(messageHelper.listApplyFriendLog(userId));
        return ProtocolManager.Converter.respPacket(resPacket, imChannelContext);
    }

}
